package org.ceteca.explica.client.management;

/**
 * @author Juan C. Díaz (juancarlosdiaz@ceteca.net) <br>
 *         Copyright (c) 2009 Fundación Centro Tecnolóxico da Carne
 * 
 * <br>
 * <br>
 *         This program is free software: you can redistribute it and/or modify
 *         it under the terms of the GNU General Public License as published by
 *         the Free Software Foundation, either version 3 of the License, or (at
 *         your option) any later version. <br/>
 *         This program is distributed in the hope that it will be useful, but
 *         WITHOUT ANY WARRANTY 
 *         <br>without even the implied warranty of
 *         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *         General Public License for more details. <br/>
 *         You should have received a copy of the GNU General Public License
 *         along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */

import javax.print.PrintService;
import javax.print.PrintServiceLookup;

import org.ceteca.explica.client.ClientContext;
import org.ceteca.explica.client.gui.common.CommonClient;
import org.ceteca.explica.core.common.BaseVO;
import org.ceteca.explica.core.management.DocumentTypeOT;
import org.ceteca.explica.core.management.InterfaceManagement;
import org.ceteca.explica.core.management.InterfaceManagementErrors;
import org.ceteca.explica.core.partners.AddressOT;
import org.ceteca.explica.core.partners.InterfacePartnersErrors;
import org.ceteca.explica.core.util.Converter;
import org.ceteca.explica.core.util.OperationResult;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Text;

public class DocumentTypeClient extends CommonClient {

	private Text entryId, entryDescription, entryVersion;
	private Button buttonAccounting, buttonModifyStock, buttonIncrementStock, buttonReturn;
	private Combo comboDocumentClass;
	private Text entryDocumentTypeReportName;
	private Combo comboDocumentTypePrinter;
	private Button buttonPreview;
	
	public DocumentTypeClient() {
		super(DocumentTypeOT.class,
				InterfaceManagement.EXPLICA_EDIT_DOCUMENT_TYPE_GLADE_PATH,
				InterfaceManagement.EXPLICA_EDIT_DOCUMENT_TYPE_DIALOG_NAME);
	}

	@Override
	public void build_extension_fields_view() {
		// TODO Auto-generated method stub

	}

	@Override
	public void buildSelectionSets() {
		// TODO Auto-generated method stub

	}

	@Override
	public BaseVO getEntityData() {

		DocumentTypeOT entity = new DocumentTypeOT();
		if (this.entEditView != null) {
			this.getWidgets();
			entity.setDescription(entryDescription.getText());
			entity.setAccountingDocument(buttonAccounting.getSelection());
			entity.setModifyStock(buttonModifyStock.getSelection());
			entity.setIncrementStock(buttonIncrementStock.getSelection());
			//entity.setPurchaseDocument(buttonPurchase.getSelection());
			//entity.setSalesDocument(buttonSales.getSelection());
			entity.setDocumentClass(comboDocumentClass.getSelectionIndex());
			entity.setReturnDocument(buttonReturn.getSelection());
			entity.setReportName(entryDocumentTypeReportName.getText());
			entity.setPrinterName(comboDocumentTypePrinter.getText());
			entity.setPreview(buttonPreview.getSelection());
			if (selectedEntity != null) {
				entity.setVersion(((DocumentTypeOT) this.selectedEntity)
						.getVersion());
				entity.setIdDocumentType(((DocumentTypeOT) this.selectedEntity)
						.getIdDocumentType());
			}
		}
		return entity;
	}

	@Override
	public void getWidgets() {
		int codError=0;
		if ((entryId = ((Text) this.getWidget(
				InterfaceManagement.EDIT_DOCUMENT_TYPE_ID_WIDGETNAME))) == null) {
			codError = 1;
		}
		if ((entryDescription = ((Text) this.getWidget(
				InterfaceManagement.EDIT_DOCUMENT_TYPE_DESCRIPTION_WIDGETNAME))) == null) {
			codError = 2;
		}
		
		if ((entryVersion = ((Text) this.getWidget(
				InterfaceManagement.EDIT_DOCUMENT_TYPE_VERSION_WIDGETNAME))) == null) {
			codError = 3;
		}
		if ((buttonAccounting = ((Button) this.getWidget(
				InterfaceManagement.EDIT_DOCUMENT_TYPE_ACCOUNTING_WIDGETNAME))) == null) {
			codError = 4;
		}
		/*
		if ((buttonPurchase = ((Button) this.getWidget(
				InterfaceManagement.EDIT_DOCUMENT_TYPE_PURCHASE_WIDGETNAME))) == null) {
			codError = 5;
		}
		if ((buttonSales = ((Button) this.getWidget(
				InterfaceManagement.EDIT_DOCUMENT_TYPE_SALES_WIDGETNAME))) == null) {
			codError = 6;
		}
		*/
		if ((buttonModifyStock = ((Button) this.getWidget(
				InterfaceManagement.EDIT_DOCUMENT_TYPE_STOCK_WIDGETNAME))) == null) {
			codError = 7;
		}
		if ((comboDocumentClass = ((Combo) this.getWidget(
				InterfaceManagement.EDIT_DOCUMENT_TYPE_CLASS_WIDGETNAME))) == null) {
			codError = 8;
		}
		if ((buttonIncrementStock = ((Button) this.getWidget(
				InterfaceManagement.EDIT_DOCUMENT_TYPE_STOCK_INCREMENT_WIDGETNAME))) == null) {
			codError = 9;
		}
		if ((buttonReturn = ((Button) this.getWidget(
				InterfaceManagement.EDIT_DOCUMENT_TYPE_RETURN_WIDGETNAME))) == null) {
			codError = 10;
		}
		if ((entryDocumentTypeReportName= ((Text)this.getWidget(
				InterfaceManagement.EDIT_DOCUMENT_TYPE_REPORTNAME_WIDGETNAME)))==null){
			codError=11;
		}
		if ((comboDocumentTypePrinter= ((Combo)this.getWidget(
				InterfaceManagement.EDIT_DOCUMENT_TYPE_PRINTER_WIDGETNAME)))==null){
			codError=12;
		}
		
		if ((buttonPreview=((Button) this.getWidget(InterfaceManagement.EDIT_DOCUMENT_TYPE_PREVIEW_WIDGETNAME))) == null){
			codError=13;
		}
		
		
		if (codError!=0){
			System.out.println("Error #"+codError);
			this.fireExceptionAndExit("Error getting widget: "+codError);
		}
	}

	@Override
	public void init_specific_components() {

	}

	@Override
	public void setEditionMode(boolean enabled) {
		getWidgets();
		entryId.setEnabled(false);
		entryVersion.setEnabled(false);
		entryDescription.setEnabled(enabled);
		buttonAccounting.setEnabled(enabled);
		buttonModifyStock.setEnabled(enabled);
		buttonPreview.setEnabled(enabled);
		entryDocumentTypeReportName.setEnabled(enabled);
		
		if (entryId.getText().equals("0") || entryId.getText().isEmpty()){
			//buttonPurchase.setEnabled(enabled);
			//buttonSales.setEnabled(enabled);
			buttonReturn.setEnabled(enabled);
			comboDocumentClass.setEnabled(enabled);
		}else{
			//buttonPurchase.setEnabled(false);
			//buttonSales.setEnabled(false);
			buttonReturn.setEnabled(false);
			comboDocumentClass.setEnabled(false);
		}
	}

	@Override
	public void setWidgetsValues(BaseVO entity) {
		DocumentTypeOT ent= (DocumentTypeOT)entity;
		getWidgets();
		entryId.setText(Converter.getInstance().toString(
				ent.getIdDocumentType()));
		entryDescription.setText(ent.getDescription() == null ? "" : ent
				.getDescription());
		entryVersion.setText(Converter.getInstance().toString(ent.getVersion()));
		buttonAccounting.setSelection(ent.isAccountingDocument());
		//buttonPurchase.setSelection(ent.isPurchaseDocument());
		//buttonSales.setSelection(ent.isSalesDocument());
		buttonModifyStock.setSelection(ent.isModifyStock());
		buttonIncrementStock.setSelection(ent.getIncrementStock());
		buttonReturn.setSelection(ent.isReturnDocument());
		buttonPreview.setSelection(ent.getPreview());
		entryDocumentTypeReportName.setText(Converter.getInstance().toString(ent.getReportName()));
		

		
		
		PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
		
		comboDocumentTypePrinter.removeAll();
		for (int i=0;i<services.length;i++){
			System.out.println(services[i].getName());
			comboDocumentTypePrinter.add(services[i].getName());
		}
		comboDocumentTypePrinter.setText(Converter.getInstance().toString(ent.getPrinterName()));
		
		if (ent.getDocumentClass()>=0 && ent.getDocumentClass()<=3)
			comboDocumentClass.select(ent.getDocumentClass());
	}

	@Override
	public OperationResult validateEntity() {
		OperationResult result = new OperationResult();
		boolean datosValidos = true;
		DocumentTypeOT entity = (DocumentTypeOT) getEntityData();
		if (!entity.isModifyStock()  && entity.isIncrementStock()
) {
			
			result
					.addResultMessage(
							OperationResult.RESULT_ERROR,
							ClientContext
									.getInstance()
									.getMsgCatalog()
									.newResultMessage(
											InterfaceManagementErrors.MSG_MANAGEMENT_DOCUMENT_TYPE_ERROR));

			datosValidos = false;
		}
		
		if (datosValidos)
			result.addResultMessage(OperationResult.RESULT_OK, ClientContext
					.getInstance().getMsgCatalog().newResultMessage(
							InterfacePartnersErrors.MSG_PARTNERS_VALIDATE_OK));
		return result;
	}

	@Override
	public void fillEntityData() {
		if (this.selectedEntity != null && this.entEditView != null) {
			getWidgets();
			setWidgetsValues((DocumentTypeOT) selectedEntity);
		}
	}	
	
	
}
